import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:learning/components/MyText.dart';

import '../IconFontIcons.dart';
import '../generated/l10n.dart';

class UserPage extends StatefulWidget {
  const UserPage({super.key});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  @override
  State<UserPage> createState() => _UserPage();
}

class _UserPage extends State<UserPage> {
  final ScrollController _control = new ScrollController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void didUpdateWidget(UserPage oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
        appBar: BrnAppBar(
          automaticallyImplyLeading: false,
          backgroundColor: Colors.white,
        ),
        body: Container(
            color: Colors.black.withAlpha(12),

            child: Column(
              children: [
                userHeaderLayout(),
              smallTestSelectedWidget()],
            )));
  }


  Widget smallTestSelectedWidget() {
    return GridView(

        padding: EdgeInsets.all(5.0),
        primary: false,
        shrinkWrap: true,
        controller: _control,
        physics: BouncingScrollPhysics(),
        // 回弹效果
        cacheExtent: 200.0,
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 150.0,
            mainAxisSpacing: 10.0,
            crossAxisSpacing: 5.0,
            childAspectRatio: 3.0 / 2.0),
        children: getMoreGridContainer());
  }
  //icon-a-huaban2fuben2

  List<Widget> getMoreGridContainer() {
    List<SmallSelectedBean> listExam = [
      SmallSelectedBean(
          title: S.of(context).beitimoshi,
          icon: IconFontIcons.iconAHuaban2fuben2,
          color: Colors.greenAccent),
      SmallSelectedBean(
          title: S.of(context).gongkenanti,
          icon: IconFontIcons.iconA2User,
          color: Colors.orangeAccent),
      SmallSelectedBean(
          title: S.of(context).suijilianxi,
          icon: IconFontIcons.iconDocument,
          color: Colors.redAccent),
      SmallSelectedBean(
          title: S.of(context).duoxuanti,
          icon: IconFontIcons.iconIcon_duoxuanti,
          color: Colors.blueAccent),
      SmallSelectedBean(
          title: S.of(context).danxuanti,
          icon: IconFontIcons.iconFuwu1,
          color: Colors.orangeAccent),
      SmallSelectedBean(
          title: S.of(context).panduanti,
          icon: IconFontIcons.iconSetting,
          color: Colors.deepOrangeAccent)
    ];
    List<Widget> list = [];
    for (int i = 0; i < listExam.length; i++) {
      SmallSelectedBean smallSelectedBeans = listExam.elementAt(i);
      print('----------------------------------------');
      print(smallSelectedBeans);
      Widget widget = smallSelecteTestWidget(smallSelectedBeans);
      list.add(InkWell(onTap: () {}, child: widget));
    }

    return list;
  }

  Widget smallSelecteTestWidget(SmallSelectedBean smallSelectedBean) {
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10))
      ),
      padding: EdgeInsets.all(10),
      child: Column(
        children: [
          Expanded(
              child: Container(
                // padding: const EdgeInsets.only(bottom: 10,top: 10 ),
                  child: Expanded(
                      child: Icon(
                        smallSelectedBean.icon,
                        size: 30,
                        color: smallSelectedBean.color,
                      )))),
          Expanded(
              child: Container(
                child: MyText(
                  smallSelectedBean.title,
                  style: TextStyle(color: Colors.black, fontSize: 12),
                ),
              )),
        ],
      ),
    );
  }

  Widget userHeaderLayout() {
    return Container(
        padding: EdgeInsets.all(10),

 color: Colors.white,
      child:ListTile(leading:CircleAvatar(
        backgroundColor: Colors.grey,
        radius: 30.0,
        //   child: Image.asset("assets/image/test/avatar.jpg"),
        backgroundImage: AssetImage('assets/image/test/avatar.jpg'), // 使用背景图
      ),title: MyText("Serkol"),subtitle: MyText("Lorem"))
    );
  }
}

class SmallSelectedBean {
  String title;
  IconData icon;
  String? url;
  Color color;

  SmallSelectedBean(
      {required this.title,
        this.icon = IconFontIcons.iconAShieldDone1,
        this.url,
        this.color = Colors.amber});

  @override
  String toString() {
    return 'SmallSelectedBean{title: $title, icon: $icon, url: $url, color: $color}';
  }
}
